package com.qianbei.common.net.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.qianbei.common.R;
import com.qianbei.common.net.control.ServerResult;

public class ProgressView extends BaseProgressView implements OnClickListener {

	private Context context;
	private View progress;
	public View errorView;
	private TextView progressText;
	private View rateView;

	public ProgressView(Context context) {
		this(context, null);
	}

	public ProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		this.setBackgroundColor(Color.parseColor("#ffffff"));
		this.setVisibility(View.GONE);

		View progressView = View.inflate(context, R.layout.comm_progress, null);
		progress = progressView.findViewById(R.id.progress);
		progressText = (TextView) progressView.findViewById(R.id.progress_text);
		rateView = progressView.findViewById(R.id.progress_rate);
		errorView = progressView.findViewById(R.id.error);
		errorView.setOnClickListener(this);
//		progressView.findViewById(R.id.close).setVisibility(View.GONE);
		this.addView(progressView);
	}

	public void showProgress(String describe) {
		if (describe == null) {
			describe = "加载中...";
		}
		progressText.setText(describe);
		progress.startAnimation(AnimationUtils.loadAnimation(context,
				R.anim.comm_anim_progress));
		errorView.setVisibility(View.GONE);
		this.setVisibility(View.VISIBLE);
		this.bringToFront();
	}

	public void showError() {
		errorView.setVisibility(View.VISIBLE);
	}

	public void progressRate(int value) {
		LayoutParams lp = (LayoutParams) rateView.getLayoutParams();
		lp.width = value * getWidth() / 100;
		rateView.setLayoutParams(lp);
	}

	public void progressFinish() {
		this.setVisibility(View.GONE);
	}
	
	@Override
	public void serverFinish(ServerResult sr) {
		super.serverFinish(sr);
		if(sr.exception!=null){
			showError();
		}
	}

	@Override
	public void onClick(View v) {
		v.setVisibility(View.GONE);
		startControl();
	}

	public void setErrorClickListener(OnClickListener l) {
		errorView.setOnClickListener(l);
	}
}
